package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.domain.Role;
import cn.congzhou.crm.mapper.RoleMapper;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        if(id!=null){
            roleMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }


    @Override
    public Role selectByPrimaryKey(Long id) {
        Role dept = roleMapper.selectByPrimaryKey(id);
        return dept;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> dept = roleMapper.selectAll();
        return dept;
    }

    @Override
    public void saveOrUpdate(Role role, Long[] ids) {
        if(role.getId()!=null){
            roleMapper.updateByPrimaryKey(role);
        }else {
            roleMapper.insert(role);
        }

//      维护关系,先删除它们之间的关系然后在添加
        roleMapper.deleteRelation(role.getId());
        if(ids!=null && ids.length!=0){
            for (Long id : ids) {
            roleMapper.insertRelation(role.getId(),id);
            }
        }

    }

    @Override
    public PageInfo<Role> selectByPage(QueryObject qo) {
//      在调用查询方法前执行分页方法，此方法有效一次有效
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.selectByPage(qo);
//        创建结果集对象返回
        PageInfo<Role> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }
}
